//
//  HRLoginRootViewModel.m
//  Hear
//
//  Created by mac on 2017/3/22.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRLoginRootViewModel.h"
#import <UMSocialCore/UMSocialCore.h>
#import "HRLoginModel.h"
#import "HRUpdataUserInfoModel.h"
#import "HUB.h"
@implementation HRLoginRootViewModel
-(void)otherLoginType:(LoginType)loginType LoginResult:(void(^)(NSDictionary *result))loginResult
{
    UMSocialPlatformType platType;
    if (loginType == WxLognin)
    {
        platType = UMSocialPlatformType_WechatSession;
    }else if (loginType == WbLogin){
        platType = UMSocialPlatformType_Sina;
    }else if (loginType == QQLogin)
    {
        platType = UMSocialPlatformType_QQ;
    }
    
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platType currentViewController:nil completion:^(id result, NSError *error) {
        if (error)
        {
            [MBProgressHUD showMessage:@"未下载客户端" finishBlock:nil];
        }else{
            UMSocialUserInfoResponse *resp = result;
            NSString *WeiXinId = @"";
            NSString *QQId = @"";
            NSString *WeiBoId = @"";
            NSString *logType = @"";
            if (loginType == WxLognin)
            {
                WeiXinId = resp.openid;
                logType = @"1";
            }else if (loginType == WbLogin){
                WeiBoId = resp.openid;
                logType = @"3";
            }else if (loginType == QQLogin)
            {
                QQId = resp.openid;
                logType = @"2";
            }

            NSDictionary *dataDict = @{                                 @"usid":resp.uid,@"WeiXinId":WeiXinId,@"WeiBoId":WeiBoId,@"QQId":QQId,@"loginType":logType,@"openId":resp.openid
                                       };
            loginResult(dataDict);
            
        }
        
        
    }];
}


-(void)phoneLoginParam:(HRLoginRequestModel *)paramModel loginSeccess:(void(^)(BOOL loginSeccess))loginSeccess{
    YTKApi *api = [YTKApi apiGetWithUrl:@"UserInfo/Get_UserLogin" parameter:paramModel];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            [[HUB shareHub] requestDidStop:@""];

            NSDictionary *dict = request.responseJSONObject;
            if ([dict[@"CodeStatus"] intValue] == 20000){
                [[NSUserDefaults standardUserDefaults] setObject:dict[@"BodyContent"] forKey:@"userInfo"];
                
                HRUpdataUserInfoModel*model = [HRUpdataUserInfoModel modelWithJSON:USERINFO];
                [[NSUserDefaults standardUserDefaults] setObject:model.LocalId forKey:@"userid"];
                [[NSUserDefaults standardUserDefaults] setObject:model.QuestionID forKey:@"QuestionID"];
                [[NSUserDefaults standardUserDefaults] setObject:model.QuestionText forKey:@"QuestionText"];
                
                [[NSUserDefaults standardUserDefaults] setObject:model.UserCoverPicture forKey:@"UserCoverPicture"];
                
                [[NSUserDefaults standardUserDefaults] setObject:model.LocalId forKey:@"userid"];
                [[NSUserDefaults standardUserDefaults] setObject:model.UserPictureName forKey:@"iconName"];
                [[NSUserDefaults standardUserDefaults] setObject:model.UserPictureBackgroundColor forKey:@"iconColor"];
                [[NSUserDefaults standardUserDefaults] setObject:model.MobileNumber forKey:@"MobileNumber"];
                [[NSUserDefaults standardUserDefaults] setObject:model.Sex forKey:@"sex"];
                [[NSUserDefaults standardUserDefaults] setObject:model.NickName forKey:@"nickName"];
                [[NSUserDefaults standardUserDefaults] setURL:[NSURL URLWithString:model.UserSoundUrl] forKey:@"SoundUrl"];
                [self loginEaseMob];
                loginSeccess(YES);

            }else{
                loginSeccess(NO);
            }
           
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            loginSeccess(NO);
        }];
    }];

}

- (void)loginEaseMob {
    HRUpdataUserInfoModel *infoModel = [HRUpdataUserInfoModel modelWithJSON:USERINFO];
    EMError *error = [[EMClient sharedClient] loginWithUsername:infoModel.LocalId password:infoModel.Password];
    if (!error){
        DDLog(@"登录聊天服务器成功");
        [[EMClient sharedClient].options setIsAutoLogin:YES];
        EMPushOptions *options = [[EMClient sharedClient] pushOptions];
        options.displayStyle = EMPushDisplayStyleMessageSummary;
        options.nickname = infoModel.NickName;
        [[EMClient sharedClient] updatePushOptionsToServer];
    }else{
        DDLog(@"登录聊天服务器失败");
    }
}


@end
